package ru.scf37;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.swing.Icon;
import javax.swing.JPanel;

public class IconListPanel extends JPanel {
	private static final long serialVersionUID = -3214315591654664931L;
	private IconListPanelModel model;
	
	public int itemHeight = 20;
	public int itemWidth = 100;
	public int itemsColWidth = 120;
	public int iconPlaceWidth = 16;
	public Color backgroundColor = Color.WHITE;
	public Color selectionColor = Color.CYAN;
	
	private int itemsPerColumn;
	private int columnCount;
	private boolean[] selectedItems;
	
	public IconListPanel() {
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				int row = y / itemHeight;
				int col = x / itemsColWidth;
				int index = row + col * itemsPerColumn;
				if (index < model.getItems().size()) {
					selectedItems[index] = !selectedItems[index];
					repaint();
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		addComponentListener(new ComponentAdapter() {
		    public void componentResized(ComponentEvent e) {
		    	modelChanged();
		    }
		    public void componentShown(ComponentEvent e) {
		    	modelChanged();
		    }
		});
	}
	
	private void modelChanged() {
		int itemsCount = model.getItems().size();
		
		itemsPerColumn = getHeight() / itemHeight;
		if (itemsPerColumn == 0)
			itemsPerColumn = 1;
		columnCount = (model.getItems().size() + itemsPerColumn - 1) / itemsPerColumn;
		selectedItems = new boolean[itemsCount];
	}
	
	@Override
	protected void paintComponent(Graphics g) {
	//	modelChanged();
		g.setColor(backgroundColor);
		
		List<IconListItem> items = model.getItems();
		

		Dimension d = getPreferredSize();
		d.width = columnCount * itemsColWidth;
		setPreferredSize(d);
		
		Iterator<IconListItem> iterator = items.iterator();
		
		g.setColor(backgroundColor);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.BLACK);
		
		int index = 0;
	done_drawing:
		for (int col = 0 ; col < columnCount ; col++) {
			for (int row = 0 ; row < itemsPerColumn ; row++) {
				if (!iterator.hasNext())
					break done_drawing;
				IconListItem item = iterator.next();
				int x = col * itemsColWidth;
				int y = row * itemHeight;
				if (selectedItems[index]) {
					Color c = g.getColor();
					g.setColor(selectionColor);
					g.fillRect(x, y, itemWidth, itemHeight);
					g.setColor(c);
				}
				Icon icon = item.getIcon();
				if (icon != null)
					item.getIcon().paintIcon(this, g, 
							x + (iconPlaceWidth - item.getIcon().getIconWidth())/2,
							y + (itemHeight - item.getIcon().getIconHeight())/2);
				int fontHeight = g.getFontMetrics().getAscent();
		//		g.drawRect(col * itemsColWidth, row * itemHeight, itemWidth, itemHeight);
		//		g.drawRect(col * itemsColWidth, row * itemHeight, itemsColWidth, itemHeight);
				g.drawString(item.getText(), x + iconPlaceWidth + 2, 
					y + fontHeight + (itemHeight - fontHeight)/2);
				index++;
			}
		}
	}

	public IconListPanelModel getModel() {
		return model;
	}

	public void setModel(IconListPanelModel model) {
		this.model = model;
		modelChanged();
	}
	
	
}
